//
//  YMModalView.m
//  XunmallManager
//
//  Created by mac on 2018/3/20.
//  Copyright © 2018年 yunmei. All rights reserved.
//

#import "YMModalView.h"
@interface  YMModalView()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,copy) NSArray *selectData;
@property (nonatomic,copy) void(^action)(NSInteger index);
@property (nonatomic,copy) NSArray * imagesData;
@end

YMModalView * backgroundView;
UITableView * tableView;
UIImageView *bgImageView;

@implementation YMModalView
- (instancetype)init{
    if (self = [super init]) {
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
+ (void)addModalViewSelectWithWindowFrame:(CGRect)frame selectData:(NSArray *)selectData images:(NSArray *)images withIs:(NSString *)isOrNo action:(void(^)(NSInteger index))action animated:(BOOL)animate {
    if (backgroundView != nil) {
        [YMModalView hiden];
    }
     CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIWindow *win = [[[UIApplication sharedApplication] windows] firstObject];
    backgroundView = [[YMModalView alloc] initWithFrame:win.bounds];
    backgroundView.action = action;
    backgroundView.imagesData = images ;
    backgroundView.selectData = selectData;
    backgroundView.userInteractionEnabled = YES;
    backgroundView.backgroundColor = [UIColor clearColor];
    [win addSubview:backgroundView];//    bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 70, (HEIGHT == 812.0?70:60) - 19.0 * selectData.count , frame.size.width, 60)];
    bgImageView = [[UIImageView alloc]init];
    if ([isOrNo isEqualToString:@"5"]) {
        
         bgImageView.image = [UIImage imageNamed:@"huangsebeijin2g(1)"];
        if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
            if (screenSize.height > screenSize.width) {
                if (screenSize.height == 568) {
                     bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(55),GTFixHeightFlaot(30 *selectData.count), frame.size.width, GTFixHeightFlaot(40));
                }else if (screenSize.height == 667) {
                     bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(21 *selectData.count), frame.size.width, GTFixHeightFlaot(40));
                }else if (screenSize.height == 736) {
                     bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(17 *selectData.count), frame.size.width, GTFixHeightFlaot(40));
                }else if (screenSize.height == 812) {
                     bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(55),GTFixHeightFlaot(42 *selectData.count), frame.size.width, GTFixHeightFlaot(40));
                }else{
                     bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(17 *selectData.count), frame.size.width, GTFixHeightFlaot(40));
                }
            }
        }
    }else {
        bgImageView.image = [UIImage imageNamed:@"huangsebeijin1g2(1)"];
        if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
            if (screenSize.height > screenSize.width) {
                if (screenSize.height == 568) {
                    bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(55),GTFixHeightFlaot(5 *selectData.count), frame.size.width, GTFixHeightFlaot(80));
                }else if (screenSize.height == 667) {
                    bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(3 * selectData.count), frame.size.width, GTFixHeightFlaot(70));
                }else if (screenSize.height == 736) {
                   bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(selectData.count), frame.size.width, GTFixHeightFlaot(70));
                }else if (screenSize.height == 812) {
                    bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(55),GTFixHeightFlaot(12 * selectData.count ), frame.size.width, GTFixHeightFlaot(70));
                }else{
                   bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(selectData.count), frame.size.width, GTFixHeightFlaot(70));
                }
            }
        }
//        bgImageView.frame = CGRectMake(kMainScreen_width - GTFixWidthFlaot(53),GTFixHeightFlaot(selectData.count), frame.size.width, GTFixHeightFlaot(70));
    }
    // 定点
    bgImageView.layer.cornerRadius = 8;
    bgImageView.userInteractionEnabled = YES;
    bgImageView.backgroundColor = [UIColor clearColor];
    bgImageView.layer.anchorPoint = CGPointMake(1.0, 0);
    bgImageView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
    [win addSubview:bgImageView];
    // TAB
     if ([isOrNo isEqualToString:@"5"]) {
         
         
         if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
             if (screenSize.height > screenSize.width) {
                 if (screenSize.height == 568) {
                      tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
                 }else if (screenSize.height == 667) {
                      tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
                 }else if (screenSize.height == 736) {
                      tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
                 }else if (screenSize.height == 812) {
                      tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
                 }else{
                      tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
                 }
             }
         }
         
         
//    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(30)) style:0];
     }else{
         
         
         if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
             if (screenSize.height > screenSize.width) {
                 if (screenSize.height == 568) {
                     tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(60)) style:0];
                 }else if (screenSize.height == 667) {
                     tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(55)) style:0];
                 }else if (screenSize.height == 736) {
                    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(55)) style:0];
                 }else if (screenSize.height == 812) {
                    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(55)) style:0];
                 }else{
                    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(55)) style:0];
                 }
             }
         }
         
         
         
//          tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,GTFixHeightFlaot(10) , frame.size.width, GTFixHeightFlaot(55)) style:0];
     }
    tableView.dataSource = backgroundView;
    tableView.delegate = backgroundView;
    tableView.layer.cornerRadius = 10.0f;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    tableView.rowHeight = 35;
    [bgImageView addSubview:tableView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundClick)];
//    tap.delegate = self;
    [backgroundView addGestureRecognizer:tap];
    backgroundView.action = action;
    backgroundView.selectData = selectData;
    if (animate == YES) {
        backgroundView.alpha = 0;
        [UIView animateWithDuration:0.2 animations:^{
            backgroundView.alpha = 0.5;
            bgImageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
    }
}
+ (void)tapBackgroundClick {
    [YMModalView hiden];
}
+ (void)hiden {
    if (backgroundView != nil) {
        
        [UIView animateWithDuration:0.2 animations:^{
            bgImageView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
        } completion:^(BOOL finished) {
            [backgroundView removeFromSuperview];
            [bgImageView removeFromSuperview];
            bgImageView = nil;
            backgroundView = nil;
        }];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _selectData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Identifier = @"YMModalViewSelectIdent";
    UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:Identifier];
    }
    if (self.imagesData) {
        cell.imageView.image = [UIImage imageNamed:self.imagesData[indexPath.row]];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = _selectData[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.action) {
        self.action(indexPath.row);
    }
    [YMModalView hiden];
}

@end
